/***
 * author:林露
 * var html =new ExchangeRate().open({maxInteger:5100,integral:2000,cash:80});
 * $("body").append(html)
 * $("body").append(new NumberPlus().open({maxInteger:5500}))
 *
 * ****/
;(function(w,d){
    function ExchangeRate(){

    }
    ExchangeRate.prototype.dom = function(){
        var $wrap = $("<div>",{"id":"price_PID","maxInteger":this.options.maxInteger}),
            pWrap = '<p class="price_PID priceItem mt10 integral">'+
                '<input type="hidden" readonly="readonly" name="integral" id="" value='+(this.options.integral*this.options.num)+' />'+
                '<span class="txt">'+(this.options.integral*this.options.num)+'</span>'+
                '<span class="plus">+</span>'+
                '<span class="min">-</span>'+
                '</p>'+
                '<p class="price_PID priceItem mt10 cash">'+
                '<input type="hidden" readonly="readonly" name="cash" id="" value='+(this.options.cash*this.options.num)+' />'+
                '<span class="txt">'+(this.options.cash*this.options.num)+'</span>'+
                '<span class="min">+</span>'+
                '<span class="plus">-</span>'+
                '</p>';
        var html = $wrap.clone().append(pWrap),
            _this = this,
            integral = $("input[name=integral]",html);

        $(" .plus",html).on("click",function(){
            _this.plus.call(_this,$(this),html,integral)
        });
        $(" .min",html).on("click",function(){
            _this.min.call(_this,$(this),html,integral)
        });
        return html ;
    };
    ExchangeRate.prototype.open = function(object1){
        this.options = $.extend(true, {
            rate:100,//汇率
            integral:5000,//积分默认值
            sum:100,//单次加的积分
            cash:50,//现金默认值
            num:1,//商品数量
            dom : null,
            maxInteger:10200,//最高积分使用
            inputName:"exchangeRate",//input name属性
            maxCash:100,//最高现金使用
            init:new Function(),//初始化函数
            plusInit:new Function(),//点击+
            callback:new Function()
        } , object1);
        var dom = this.dom.call(this),
            _this = this;
        this.options.init(dom);
        this.options.dom = dom;
        return dom;
    }
    ExchangeRate.prototype.set = function(e){
        var integral = this.options.integral * e,
            cash = this.options.cash * e,
            html = this.options.dom;
        $(".integral .txt",html).html(integral);
        $(".cash .txt",html).html(cash);
        $("input[name=cash]",html).val(cash);
        $("input[name=integral]",html).val(integral);
        this.options.callback(cash,integral);
        return this	;
    };
    ExchangeRate.prototype.plus = function(that,dom,integral,cash){
        var cash  = $("input[name=cash]",dom);
        if(integral.val() < this.options.maxInteger ){
            integralVal = integral.val()*1+this.options.sum ;
            integral.val(integralVal);
            $(".integral .txt",dom).html(integral.val());
            var newCash = cash.val()*1 -  this.options.sum / this.options.rate;
            cash.val(newCash)
            $(".cash .txt",dom).html(newCash);
            this.options.callback(newCash,integralVal);
        }else{
            alert("最多兑换"+this.options.maxInteger+"积分")
        }
    };
    ExchangeRate.prototype.min = function(that,dom,integral,cash){
        var cash  = $("input[name=cash]",dom),integralVal = integral.val();
        if(integralVal > 0){
            integral.val(integralVal-this.options.sum);
            $(".integral .txt",dom).html(integralVal);
            var newCash = cash.val()*1 + this.options.sum / this.options.rate ;
            cash.val(newCash)
            $(".cash .txt",dom).html(newCash);
            this.options.callback(newCash,integralVal);
        }else{
            alert("不能再少了");
        }
    };

    w.ExchangeRate = ExchangeRate;
})(window,jQuery);


;(function(w,j){
    function NumberPlus(){

    }
//	NumberPlus.prototype.dom = function(){
//		var _this = this,
//			$wrap = $("<p>",{"class":"price_PID priceItem mt10 number","maxInteger":this.options.min}),
//			$input = $("<input>",{"type":"hidden","val":this.options.val}),
//			$txt = $("<span>",{"class":"txt","text":this.options.val}),
//			$plus = $("<span>",{"class":"plus","text":"+"}).on("click",function(){
//						_this.plus.call(_this,$(this),$wrap,$input,$txt)
//					}),
//					$min = $("<span>",{"class":"min","text":"-"}).on("click",function(){
//					_this.min.call(_this,$(this),$wrap,$input,$txt)
//			});
//			var html = $wrap.append($input,$txt,$plus,$min);
//			return html ;
//	};
    NumberPlus.prototype.open = function(object1){
        this.options = $.extend(true, {
            min:1,//最小数量
            max:5,//最大数量
            val:2,//默认值
            sum:1,//单次加减值
            inputName:"number",//input name值
            dom:null,
            callback:new Function
        } , object1);

        var _this = this,
            $wrap = $("<p>",{"class":"price_PID priceItem mt10 number","maxInteger":this.options.min}),
            $input = $("<input>",{"type":"hidden","val":this.options.val,"name":this.options.inputName}),
            $txt = $("<span>",{"class":"txt","text":this.options.val}),
            $plus = $("<span>",{"class":"plus","text":"+"}).on("click",function(){
                _this.plus.call(_this,$(this),$wrap,$input,$txt)
            }),
            $min = $("<span>",{"class":"min","text":"-"}).on("click",function(){
                _this.min.call(_this,$(this),$wrap,$input,$txt)
            });
        var dom = $wrap.append($input,$txt,$plus,$min);
        this.options.dom = dom;
        return dom;
    }
    NumberPlus.prototype.plus = function(that,dom,inputNum,txt){
        if(inputNum.val() < this.options.max ){
            var numVal = inputNum.val() * 1 +this.options.sum;
            inputNum.val(numVal);
            txt.html(inputNum.val());
            this.options.callback(numVal);
        }else{
            alert("最多添加"+this.options.max+"件");
        }
    };
    NumberPlus.prototype.set = function(e){
        var html = this.options.dom;
        $(".integral .txt",html).html(integral);
        $(".cash .txt",html).html(cash);
        $("input[name=cash]",html).val(cash);
        $("input[name=integral]",html).val(integral);
        return this	;
    };
    NumberPlus.prototype.min = function(that,dom,inputNum,txt){
        if(inputNum.val() > this.options.min){
            var numVal = inputNum.val()-this.options.sum;
            inputNum.val(numVal);
            txt.html(numVal);
            this.options.callback(numVal);
        }else{
            alert("数量不能低于"+this.options.min+"件")
        }
    };

    w.NumberPlus = NumberPlus;
})(window,jQuery);